 package com.gy.algorithm.carl.stackqueueexa;

 import java.util.ArrayDeque;
 import java.util.Deque;

 /**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class RemoveDuplicates1047 {

    public static void main(String[] args) {

    }

    // end
//    public static String removeDuplicates(String s) {
//
//    }
    // write
    public static String removeDuplicates(String s) {
        Deque<Character> stack = new ArrayDeque<>();

        for (int i = 0; i < s.length(); i++) {
            if(!stack.isEmpty() && stack.peekLast() == s.charAt(i)){
                stack.removeLast();
            }else{
                stack.addLast(s.charAt(i));
            }
        }

        String res = "";
        while (!stack.isEmpty()){
            res += stack.removeFirst();
        }

        return res;
    }
}
